package lk.icta.core.entity;

import lk.icta.common.AppConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.io.Serializable;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(schema = "ELAND_RAIL")
@NamedQueries({
        @NamedQuery(
                name = "getAlienationMethodById",
                query = "SELECT am FROM AlienationMethod am WHERE am.id = :id"
        ),
        @NamedQuery(
                name = "getAllUpdatableAlienationMethods",
                query = "SELECT am FROM AlienationMethod am WHERE am.state > 2 "
        )
})
public class AlienationMethod implements Serializable {

    public enum State {
        REJECTED,   // 0 - state. Not available for alienation
        APPROVED,   // 1 - state. Available for alienation
        PENDING,    // 2 - state. Pending for approval and available for alienation
        DATA_ENTRY  // 3 - state. Land is in data entry state.
    }

    private static final Logger logger = LoggerFactory.getLogger(AlienationMethod.class);

    /**
     * A system generated unique key
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    /**
     * The Name of the Alienation Method
     */
    @Column(nullable = false, unique = true, updatable = true)
    private String siName;
    @Column(nullable = false, unique = true, updatable = true)
    private String enName;
    @Column(nullable = false, unique = true, updatable = true)
    private String taName;

//    @Transient
//    private EnumSet<WorkflowTask> workflow1;

    @ManyToMany
    @JoinTable(schema = "ELAND_RAIL")
    private List<WorkflowItem> workflowItem;

    /**
     * Land Usages per Alienation Method
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(schema = "ELAND_RAIL")
    private Set<LandUsage> landUsage;

    @Column
    private String purpose;

    @Column
    private String standards;

    @Column
    private State state;

    @Embedded
    private LifeCycleInfo lifeCycleInfo = new LifeCycleInfo();


    public AlienationMethod() {
    }

    public AlienationMethod(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSiName() {
        return siName;
    }

    public void setSiName(String siName) {
        this.siName = siName;
    }

    public String getEnName() {
        return enName;
    }

    public void setEnName(String enName) {
        this.enName = enName;
    }

    public String getTaName() {
        return taName;
    }

    public void setTaName(String taName) {
        this.taName = taName;
    }


    public Set<LandUsage> getLandUsage() {
        return landUsage;
    }

    public void setLandUsage(Set<LandUsage> landUsage) {
        this.landUsage = landUsage;
    }

 /*   public EnumSet<WorkflowTask> getWorkflow1() {
        logger.debug("setting workflow1 of Alienation method id : {}", this.id);
        //TODO : complete this method

        switch (this.id) {
            case AppConstants.LAND_PERMIT:
                logger.debug("setting workflow1 : land Permit");
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        // WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC);
                break;

            case AppConstants.LAND_GRANT:
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        // WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC,
                        WorkflowTask.APPROVED_BY_LCG);
                //WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
                break;

            case AppConstants.SHORT_TERM_LEASE:  // This is the Anual lease flow, There is no workflow for Yeild Permit
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        // WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC);
                break;

            case AppConstants.LONG_TERM_LEASE:
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        // WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC,
                        WorkflowTask.APPROVED_BY_LCG,
                        WorkflowTask.APPROVED_BY_MINISTRY);
                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
                break;

            case AppConstants.RELEASING_TO_DEPARTMENTS:
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        //WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC,
                        WorkflowTask.APPROVED_BY_LCG);
                break;

            case AppConstants.VESTING_ORDER:
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        //WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC,
                        WorkflowTask.APPROVED_BY_LCG,
                        WorkflowTask.APPROVED_BY_MINISTRY);
                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
                break;

            case AppConstants.SPECIAL_GRANT:
                //free grants
                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
                        // WorkflowTask.APPROVED_BY_ORDS,
                        WorkflowTask.APPROVED_BY_PLC,
                        WorkflowTask.APPROVED_BY_LCG,
                        WorkflowTask.APPROVED_BY_MINISTRY);
                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
                *//* WorkflowTask.GRANTED_BY_LCG,
                WorkflowTask.GRANTED_BY_MINISTRY,
                WorkflowTask.GRANTED_BY_PRESIDENT_SECRETERY);*//*

                //For members of forces
                *//*  this.workflowItem = EnumSet.of(WorkflowTask.COMPLETED,
                // WorkflowTask.APPROVED_BY_ORDS,
                WorkflowTask.APPROVED_BY_PLC,
                WorkflowTask.APPROVED_BY_LCG,
                WorkflowTask.APPROVED_BY_MINISTRY);*//*
                //  WorkflowTask.GRANTED_BY_LCG,
                //  WorkflowTask.GRANTED_BY_MINISTRY);
                // WorkflowTask.GRANTED_BY_PRESIDENT_SECRETERY);
                break;
        }
        return workflow1;
    }*/

//    public List<WorkflowItem> getWorkflow299() {
//        logger.debug("setting workflowItem of Alienation method id : {}", this.id);
//        //TODO : complete this method
//
//        switch (this.id) {
//            case AppConstants.LAND_PERMIT:
//                logger.debug("setting workflowItem : land Permit");
//                this.workflowItem = EnumSet.of(WorkflowTask.COMPLETED,
//                        // WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC);
//                break;
//
//            case AppConstants.LAND_GRANT:
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        // WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC,
//                        WorkflowTask.APPROVED_BY_LCG);
//                //WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
//                break;
//
//            case AppConstants.SHORT_TERM_LEASE:  // This is the Anual lease flow, There is no workflow for Yeild Permit
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        // WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC);
//                break;
//
//            case AppConstants.LONG_TERM_LEASE:
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        // WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC,
//                        WorkflowTask.APPROVED_BY_LCG,
//                        WorkflowTask.APPROVED_BY_MINISTRY);
//                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
//                break;
//
//            case AppConstants.RELEASING_TO_DEPARTMENTS:
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        //WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC,
//                        WorkflowTask.APPROVED_BY_LCG);
//                break;
//
//            case AppConstants.VESTING_ORDER:
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        //WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC,
//                        WorkflowTask.APPROVED_BY_LCG,
//                        WorkflowTask.APPROVED_BY_MINISTRY);
//                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
//                break;
//
//            case AppConstants.SPECIAL_GRANT:
//                //free grants
//                this.workflow1 = EnumSet.of(WorkflowTask.COMPLETED,
//                        // WorkflowTask.APPROVED_BY_ORDS,
//                        WorkflowTask.APPROVED_BY_PLC,
//                        WorkflowTask.APPROVED_BY_LCG,
//                        WorkflowTask.APPROVED_BY_MINISTRY);
//                // WorkflowTask.APPROVED_BY_PRESIDENT_SECRETERY);
//                /* WorkflowTask.GRANTED_BY_LCG,
//                WorkflowTask.GRANTED_BY_MINISTRY,
//                WorkflowTask.GRANTED_BY_PRESIDENT_SECRETERY);*/
//
//                //For members of forces
//                /*  this.workflowItem = EnumSet.of(WorkflowTask.COMPLETED,
//                // WorkflowTask.APPROVED_BY_ORDS,
//                WorkflowTask.APPROVED_BY_PLC,
//                WorkflowTask.APPROVED_BY_LCG,
//                WorkflowTask.APPROVED_BY_MINISTRY);*/
//                //  WorkflowTask.GRANTED_BY_LCG,
//                //  WorkflowTask.GRANTED_BY_MINISTRY);
//                // WorkflowTask.GRANTED_BY_PRESIDENT_SECRETERY);
//                break;
//        }
//
//        return workflowItem;
//    }


    public List<WorkflowItem> getWorkflowItem() {
        return workflowItem;
    }

    public void setWorkflowItem(List<WorkflowItem> workflowItem) {
        this.workflowItem = workflowItem;
    }

    public void setPurpose(String purpose){
        this.purpose=purpose;
    }

    public String getPurpose(){
        return purpose;
    }

    public void setStandards(String standards){
        this.standards=standards;
    }

    public String getStandards(){
        return standards;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public LifeCycleInfo getLifeCycleInfo() {
        return lifeCycleInfo;
    }

    public void setLifeCycleInfo(LifeCycleInfo lifeCycleInfo) {
        this.lifeCycleInfo = lifeCycleInfo;
    }
}

